package com.example.team.controller;

import com.example.team.entity.Attendance;
import com.example.team.service.IAttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
/****
 * 编写者：王轩
 * 时间：2023/10/27
 * 打卡信息
 * 上下班打卡功能
 */
@RestController
@Api(tags = "打卡信息管理")
public class AttendanceController {
    @Autowired
    private IAttendanceService attendanceService;
    @RequestMapping(value = "/Attendance",method = {RequestMethod.GET})
    public String Attendance(){
        return "Attendance.html";
    }
    //上班开始打卡，输入员工身份id进行打卡
    @ApiOperation(value = "上班打卡")
    @RequestMapping(value = "/insterAttendance",method = {RequestMethod.GET})
    public Integer insterAttendance(Integer EmployeeID){
        Attendance attendance=new Attendance();
        attendance.setEmployeeID(EmployeeID);
        //获取当前时间
        Date currentTime=new Date();
        //将当前上班时间添加到Attendance中
        attendance.setClockInTime(currentTime);
        try {
            // 获取IP地址
            InetAddress ipAddress = InetAddress.getLocalHost();
            String ip = ipAddress.getHostAddress();
            // 将IP地址设置到Attendance对象中
            attendance.setIP(ip);
        } catch (UnknownHostException e) {
            // 处理异常
            e.printStackTrace();
        }
        return attendanceService.insertAttendance(attendance);
    }
    //下班开始签退，用员工id和上班打卡时间为依据
    @ApiOperation(value = "下班打卡")
    @RequestMapping(value = "/outAttendance",method = {RequestMethod.GET})
    public Integer outAttendance(Integer EmployeeID){
        Attendance attendance=new Attendance();
        attendance.setEmployeeID(EmployeeID);
        //获取当前时间
        Date currentTime=new Date();
        //将当前下班时间添加到Attendance中
        attendance.setClockOutTime(currentTime);
        return attendanceService.updateTime(attendance);

    }

    //对打卡信息进行管理
    //删除方法
    @ApiOperation(value = "删除打卡信息")
    @RequestMapping(value = "/deletAttendance",method = {RequestMethod.GET})
    public Integer delectAttendance(Integer attendanceID){
        Attendance attendance=new Attendance();
        attendance.setAttendanceID(attendanceID);
        return attendanceService.deleteAttendance(attendance);
    }
    //修改方法
    @ApiOperation(value = "修改打卡信息")
    @RequestMapping(value = "/updateAttendance",method = {RequestMethod.GET})
    public int updateAttendance(Attendance attendance){
        return attendanceService.update(attendance);
    }
    //查询方法
    @ApiOperation(value = "查询所有打卡信息")
    @RequestMapping(value = "/getAllAttendance",method = {RequestMethod.GET})
    public List<Attendance> getAllAttendance(Attendance attendance){
        return attendanceService.getAllAttendance(attendance);
    }
    //查询单个条件
    @ApiOperation(value = "查询打卡信息")
    @RequestMapping(value = "/getAttendance",method = {RequestMethod.GET})
    public Attendance getAttendanceID(Integer AttendanceID){
        Attendance attendance=new Attendance();
        attendance.setAttendanceID(AttendanceID);
        return attendanceService.getAttendance(attendance);
    }
}
